<?php
namespace Videos\Controller;
use Common\Controller\AdminbaseController;
class SettingController extends AdminbaseController{
	
	protected $options_model;
	
	function _initialize() {
		parent::_initialize();
		$this->options_model = D("Common/SystemOptions");
	}

	// 阿里云OSS配置
	function aliyunoss(){
        $this->display();
	}

    // 七牛云配置
    function qiniuyun(){
        $this->display();
    }


    // 万事无忧 wsview
    function wsview(){
        C(S('sp_dynamic_config'));
        $option=$this->options_model->where("option_name='site_options'")->find();
        $cmf_settings=$this->options_model->where("option_name='cmf_settings'")->getField("option_value");
        $tpls=sp_scan_dir(C("SP_TMPL_PATH")."*",GLOB_ONLYDIR);
        $noneed=array(".","..",".svn");
        $tpls=array_diff($tpls, $noneed);
        $this->assign("templates",$tpls);

        // 万事无忧的配置
        $wsview=array(
            name=>C('wsviewname'),
            pass=>C('wsviewpass'),
        );

        $adminstyles=sp_scan_dir("public/simpleboot/themes/*",GLOB_ONLYDIR);
        $adminstyles=array_diff($adminstyles, $noneed);
        $this->assign("adminstyles",$adminstyles);
        if($option){
            $this->assign((array)json_decode($option['option_value']));
            $this->assign("option_id",$option['option_id']);
        }

        // 万事无忧
        $this->assign("wsview",$wsview);
        $this->assign("cmf_settings",json_decode($cmf_settings,true));
        $this->display();
    }

	function site_post(){
		if (IS_POST) {
			if(isset($_POST['option_id'])){
				$data['option_id']=intval($_POST['option_id']);
			}

			// 万事无忧 wsview
            $configs["wsviewname"]=$_POST['options']['wsviewname'];
            $configs["wsviewpass"]=$_POST['options']['wsviewpass'];

			sp_set_dynamic_config($configs);
				
			$data['option_name']="site_options";
			$data['option_value']=json_encode($_POST['options']);
			if($this->options_model->where("option_name='site_options'")->find()){
				$r=$this->options_model->where("option_name='site_options'")->save($data);
			}else{
				$r=$this->options_model->add($data);
			}
			
			$banned_usernames=preg_replace("/[^0-9A-Za-z_\x{4e00}-\x{9fa5}-]/u", ",", $_POST['cmf_settings']['banned_usernames']);
			$_POST['cmf_settings']['banned_usernames']=$banned_usernames;

			sp_set_cmf_setting($_POST['cmf_settings']);
			if ($r!==false) {
				$this->success("保存成功！");
			} else {
				$this->error("保存失败！");
			}
			
		}
	}


	
}